<template>
    <div>
     
        <van-cell-group inset>
          <van-field
            v-model="name"
            name="名称"
            label="名称"
            placeholder="分类名称"
          />
          <van-field
            v-model="explain"
            name="描述"
            label="描述"
            placeholder="分类描述"
          />
          <div class="van-hairline--bottom"></div>
          <!-- <div class="my_divider"></div> -->
        </van-cell-group>
        <div style="margin: 16px">
          <van-button round block type="warning" @click="methods.onSubmits()">
            编辑
          </van-button>
        </div>
      
    </div>
  </template>
  
  <script setup>
  import { addGoodsType, editType } from "@/api/manage";
  import { load } from "@/utils/load";
  import { showConfirmDialog, showToast } from "vant";
  import { onMounted, reactive, ref } from "vue";
import { useRouter } from "vue-router";
  const name = ref("");
  const explain = ref("");
  const router=useRouter();
const id=ref("");
  const methods = {
    onSubmits: async (values) => {
      if (!name.value) {
        showToast("名称不能为空");
        return;
      }
      if (!explain.value) {
        showToast("描述不能为空");
        return;
      }
      
      showConfirmDialog({
        title: "提示",
        message: "确认修改吗?",
      })
        .then(async () => {
          // on confirm
          load();
          const res = await editType({
            id:id.value,
            name: name.value,
            explain: explain.value,
          });
          console.log(2222222222);
          console.log(res,"kkkkkkkkkkkkk");
          if (res.result === 0) {
            showToast("修改成功");
            router.go(-1);
          } else {
            showToast(res.message);
          }
        })
        .catch(() => {
          // on cancel
          // console.log(222222)
        });
      // return;
    },
   
  };
  onMounted(() => {
    const type =  JSON.parse(sessionStorage.getItem("type"));
    name.value=type.name;
    explain.value=type.explain;
    id.value=type.id
    })

  
  </script>
  
  <style  scoped>
  </style>